package com.robot.service.impl;

import com.robot.common.enums.IntegralTypeEnum;
import com.robot.common.enums.KnowledgeTypeEnum;
import com.robot.common.enums.ResponseEnum;
import com.robot.common.exception.BusinessException;
import com.robot.dao.dto.IntegralDTO;
import com.robot.dao.dto.IntegralOperateDTO;
import com.robot.dao.entity.Company;
import com.robot.dao.entity.Integral;
import com.robot.dao.entity.IntegralLog;
import com.robot.dao.entity.User;
import com.robot.dao.mapper.CompanyMapper;
import com.robot.dao.mapper.IntegralLogMapper;
import com.robot.dao.mapper.IntegralMapper;
import com.robot.dao.mapper.UserMapper;
import com.robot.dao.vo.CompanyVO;
import com.robot.dao.vo.IntegralVO;
import com.robot.service.IntegralService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class IntegralServiceImpl implements IntegralService {

    @Autowired
    private IntegralMapper integralMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private IntegralLogMapper integralLogMapper;

    @Override
    public IntegralVO queryIntegral(IntegralDTO integralDTO) {

        Integral integral=this.getIdByAccountType(integralDTO.getAccountId());
        IntegralVO integralVO=new IntegralVO();
        BeanUtils.copyProperties(integral,integralVO);
        return integralVO;
    }

    @Override
    @Transactional
    public void operateIntegral(IntegralOperateDTO integralOperateDTO) {

        Integral integral=this.getIdByAccountType(integralOperateDTO.getAccountId());

        IntegralLog integralLog=IntegralLog.builder()
                .userId(Long.valueOf(integralOperateDTO.getAccountId()))
                .integralScore(integralOperateDTO.getIntegralScore())
                .integralId(integral.getId())
                .robotId(Long.valueOf(integralOperateDTO.getRobotId()))
                .remark(integralOperateDTO.getRemark())
                .type(integralOperateDTO.getOperateType())
                .build();

        if(ObjectUtils.isEmpty(integralOperateDTO.getOperateType())){
            throw new BusinessException(ResponseEnum.ILLEGAL_PARAMETER.getCode(), "请传入操作类型（10-增加，20-扣减）！");
        }
        if(integralOperateDTO.getOperateType().equals(IntegralTypeEnum.MINUS.getCode())){
            int num;
            if(integralOperateDTO.getIntegralScore()>integral.getIntegralScore()){
                num=0;
            }else{
                num=integral.getIntegralScore()- integralOperateDTO.getIntegralScore();
            }
            integral.setIntegralScore(num);
            integralMapper.updateById(integral);
            integralLogMapper.insert(integralLog);
        }else if (integralOperateDTO.getOperateType().equals(IntegralTypeEnum.ADD.getCode())){
            int num=integral.getIntegralScore()+integralOperateDTO.getIntegralScore();
            integral.setIntegralScore(num);
            integralMapper.updateById(integral);
            integralLogMapper.insert(integralLog);
        }else{
            throw new BusinessException(ResponseEnum.ILLEGAL_PARAMETER.getCode(), "传入操作类型有误（10-增加，20-扣减）！");

        }

    }

    @Override
    public Boolean isIntegralEnough(Integer userId) {
        Integral integral=this.getIdByAccountType(userId);
        if(integral.getIntegralScore()<=0){
            throw new BusinessException(ResponseEnum.ILLEGAL_PARAMETER.getCode(), "您的积分不够，此功能暂无法使用。请充值积分，畅享更多服务");
        }
        return true;
    }




    private Integral getIdByAccountType(Integer accountId) {
        accountId=39;
        if (ObjectUtils.isEmpty(accountId)) {
            throw new BusinessException(ResponseEnum.ILLEGAL_PARAMETER.getCode(), "请传入账户Id！");
        }
        User user = userMapper.selectById(accountId);
        if (ObjectUtils.isEmpty(user)) {
            throw new BusinessException(ResponseEnum.ILLEGAL_PARAMETER.getCode(), "未查询到用户信息！");
        }

        Integral integral;
        if (StringUtils.isBlank(user.getCompanyCode())) {
            integral = integralMapper.selectById(user.getIntegralId());
        } else {
            Company company = companyMapper.selectByCode(user.getCompanyCode());
            if(ObjectUtils.isEmpty(company)||ObjectUtils.isEmpty(company.getIntegralId())){
                throw new BusinessException(ResponseEnum.ILLEGAL_PARAMETER.getCode(), "未查询到公司积分相关信息！");
            }
            integral = integralMapper.selectById(company.getIntegralId());
        }

        if (ObjectUtils.isEmpty(integral)) {
            throw new BusinessException(ResponseEnum.ILLEGAL_PARAMETER.getCode(), "未查询到积分账户信息！");
        }
        return integral;
    }

}
